package com.zhongge.stackrealizequeue;

import java.util.Stack;

/**
 * @ClassName MyQueue
 * @Description TODO 栈实现队列
 * @Author zhongge
 * @Date 2025/8/25 17:41
 * @Version 1.0
 */
@
SuppressWarnings(value = {"all", "unchecked"})
public class MyQueue {
    //两个栈
    Stack<Integer> stack1;//入队
    Stack<Integer> stack2;//出队

    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int x) {
       stack1.push(x);
    }

    public int pop() {
       if (stack2.isEmpty()) {
           if (stack1.isEmpty()) {
               return -1;
           }
           int size = stack1.size();
           while (size > 0) {
               stack2.push(stack1.pop());
               size--;
           }
       }
       return stack2.pop();
    }

    public int peek() {
        if (stack2.isEmpty()) {
            if (stack1.isEmpty()) {
                return -1;
            }
            int size = stack1.size();
            while (size > 0) {
                stack2.push(stack1.pop());
                size--;
            }
        }
        return stack2.peek();
    }

    public boolean empty() {
        if (stack1.isEmpty() && stack2.isEmpty()) {
            return true;
        }
        return false;
    }
}
